/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.hlingsoft.kkh.filter;

import com.hlingsoft.kkh.constant.SystemConstant;
import com.hlingsoft.kkh.entity.Admin;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 管理过滤器
 *
 * @author Herbert
 */
public class AdminFilter implements Filter {

    protected final Logger logger = Logger.getLogger(this.getClass());

    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        request.setCharacterEncoding("UTF-8");
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Admin admin = (Admin) request.getSession().getAttribute(
                SystemConstant.SESSION_ADMIN);
        if (admin == null) {
            String path = request.getContextPath();
            String basePath = request.getScheme() + "://"
                    + request.getServerName() + ":" + request.getServerPort()
                    + path;
            response.sendRedirect(basePath + "/auth/admin/login.htm");
        } else {
            chain.doFilter(request, response);
        }
    }

    public void destroy() {
        // TODO Auto-generated method stub

    }

}
